

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.entity.ConfigLogin;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.SuggAnswer;
import net.betou.trade.main.dao.SuggAnswerDao;
import net.betou.trade.main.manager.SuggAnswerMng;
import net.betou.trade.role.setting.CommonFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author O.Y.
 */
@Service
@Transactional
public class SuggAnswerMngImpl implements SuggAnswerMng{

	public SuggAnswer deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	public SuggAnswer [ ] deleteByIds( Integer [ ] ids ) {
		SuggAnswer [ ] beans = new SuggAnswer [ ids.length ];

		for ( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}
	
	public int deleteBySuggIds( Integer [ ] ids ){
		int num = 0;
		if(ids!=null){
			for(Integer id : ids){
				List<SuggAnswer> answerList = dao.findOnSugg(id);
				for(SuggAnswer sa : answerList){
					dao.deleteById(sa.getId());
					num++;
				}
			}
		}
		return num;
	}

	@Transactional( readOnly = true )
	public SuggAnswer findById( Integer id ) {
		return dao.findById( id );
	}

	@Transactional( readOnly = true )
	public List<SuggAnswer> findOnSugg( Integer id ){
		return dao.findOnSugg( id );
	}

	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ) {
		boolean system = admin.getSystem( );

		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}else{
			
			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_CONTENT );
			
			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
			}else{
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}
		
		return pagination;
		
	}

	public SuggAnswer save( SuggAnswer bean, AgentAndAdmins admin ) {
		bean.setAdmin( admin );
		return dao.save( bean );
	}

	public SuggAnswer update( SuggAnswer bean ) {
		
		
		Updater< SuggAnswer > updater = new Updater< SuggAnswer >( bean, Updater.UpdateMode.MIN );
		
		updater.include( "title" ); // 使用最小模式，只更新名称，其余全都不更新
		updater.include( "content" );

		return dao.updateByUpdater( updater );
	}
	
	@Autowired
	private SuggAnswerDao dao;

}
